
		
		$(function(){
			
			listMenus();
		});
		
		/**
		 * 加载所有菜单
		 * @returns
		 */
		function listMenus(){
			
			//$.ajaxSettings.async = false; 
			$.getJSON(baseURL + "sys/menu/list", function(data){

				// 加载菜单
				var list = sortMenu(data);
				var menus = {"menus":list};
				var html = template('menus',menus);  
		        $('#menuTable tbody').html(html);
		        initTreeGrid();
			});
		}
		/**
		 * 初始化树形菜单
		 * @returns
		 */
		function initTreeGrid(){
			
			$('#menuTable').treegrid({
				treeColumn: 2,
				initialState:"collapsed" 
			});
		}
		/**
		 * 菜单排序
		 * @param list
		 * @returns
		 */
		function sortMenu(data){
			
			var parentMenu = [];	// 父级菜单
			var sonMenu = [];		// 子菜单
			var powerMenu = [];		// 权限菜单
			for (var i in data){
				if (data[i].type == 0){
					parentMenu.push(data[i]);
				}
				if (data[i].type == 1){
					sonMenu.push(data[i]);
				}
				if (data[i].type == 2){
					powerMenu.push(data[i]);
				}
			}
			var sonMenuSub = [];
			var parentMenuSub = [];
			for (var i in parentMenu){
				parentMenuSub = [];
				for (var j in sonMenu){
					
					sonMenuSub = [];
					for (var k in powerMenu){
						if (powerMenu[k].parentId == sonMenu[j].id){
							powerMenu[k].parentName = sonMenu[j].name;
							sonMenuSub.push(powerMenu[k]);
						}
					}
					sonMenu[j].sonMenuSub = [];
					$.merge(sonMenu[j].sonMenuSub,sonMenuSub);
					
					
					if (sonMenu[j].parentId == parentMenu[i].id){
						sonMenu[j].parentName = parentMenu[i].name;
						parentMenuSub.push(sonMenu[j]);
					}
					
				}
				parentMenu[i].parentMenuSub = [];
				$.merge(parentMenu[i].parentMenuSub,parentMenuSub);
			}
			return parentMenu;
		}
		
		$('#menuTable tbody').on('click', 'tr', function () {
			// 取消选中
			$('#menuTable tbody tr').removeClass('info');
			if($(this).find('input[type="radio"]').prop("checked")){
				$(this).find('input[type="radio"]').prop('checked','');
				return ;
			}	 
			// 选中
			$(this).find('input[type="radio"]').prop('checked','checked');
			$(this).addClass('info');
		});
		
		/**
		 * 通用的layer弹窗
		 */
		function layer_show(title,url,w,h, idFlag){
			
			if (title == null || title == '') {
				title=false;
			};
			if (url == null || url == '') {
				layer.mdg('url不能为空！')
				//url="404.html";
				return ;
			};
			if (w == null || w == '') {
				w=800;
			};
			if (h == null || h == '') {
				h=($(window).height() - 50);
			};
			// 是否需要获取选中行id
			var id;
			if (idFlag){
				id = getCheckId();
				if(!id){
					layer.msg("请选择一条记录！");
					return ;
				}
				url += "?id=" + id;
			}
			var index = layer.open({
				type: 2,
				area: [w+'px', h +'px'],
				fix: false,
				maxmin: true,
				shade:0.4,
				title: title,
				content: url
			});
		}
		
		/**
		 * 删除
		 */
		function layer_del(){
			
			layer.confirm('确定要删除选中的记录?', function(index){
				
				$.ajax({
                    type: "POST",
                    url: baseURL + "sys/menu/delete",
                    data: "id=" + getCheckId(),
                    success: function(data){
                        if(data.code === 0){
                        	delCheckRow();
            			    layer.msg('删除成功！');
            			    layer.close(index);
                        }else{
                            alert(data.msg);
                        }
                    }
                });
				
				
			});       
		}
		
		/**
		 * 删除选中的行
		 */
		function delCheckRow(){
			
			$('input[type="radio"]:checked').parents("tr").remove();
		}
		
		/**
		 * 获取选中行id
		 */
		function getCheckId(){
			
			var id = $('input[type="radio"]:checked').val();
			return id;
		}